package com.molyfun.parents.fragment.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.calendar.CalendarDay;
import com.molyfun.parents.view.calendar.MaterialCalendarView;
import com.molyfun.parents.view.calendar.OnDateSelectedListener;

import java.util.Date;

public class DateChooseDialogFragment extends DialogFragment implements View.OnClickListener{
    private DateChooseDialogClickListener mDateChooseDialogClickListener;

    public static final String FLAG_DATE_CHOOSED = "choosed_date";

    public static final String FLAG_DATE_PAGE = "date_page";

    private MaterialCalendarView mMaterialCalendarView;

    private int currentPage;

    private static DateChooseDialogFragment newInstance(String date, int currentPage) {
        DateChooseDialogFragment mDateChooseDialogFragment = new DateChooseDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(FLAG_DATE_CHOOSED, date);
        bundle.putInt(FLAG_DATE_PAGE, currentPage);
        mDateChooseDialogFragment.setArguments(bundle);
        return mDateChooseDialogFragment;
    }

    public void setAlertDialogClickListener(DateChooseDialogClickListener listener) {
        this.mDateChooseDialogClickListener = listener;
    }

    @Override public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (null != dialog) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.fragment_dialog_date, container, false);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

        String choosedDate = (String)getArguments().get(FLAG_DATE_CHOOSED);
        if(!TextUtils.isEmpty(choosedDate)){
            Date date = TimeUtils.getDateByStr(choosedDate);
            mMaterialCalendarView = new MaterialCalendarView(getContext(), getArguments().getInt(FLAG_DATE_PAGE));//(MaterialCalendarView)view.findViewById(R.id.calendarView);
            mMaterialCalendarView.setCurrentDate(date);
            mMaterialCalendarView.setDateSelected(date, true);
            mMaterialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
                @Override
                public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                    if(mDateChooseDialogClickListener != null){
                        mDateChooseDialogClickListener.onDateChange(formatDate(date.getYear(), date.getMonth() + 1, date.getDay()));
                    }
                    getDialog().dismiss();
                }
            });

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0 , getResources().getDimensionPixelOffset(R.dimen.common_normal_padding) , 0);
            view.addView(mMaterialCalendarView, layoutParams);
        }

        return view;
    }

    private String formatDate(int year, int month, int day){
        StringBuilder sb = new StringBuilder();
        sb.append(year);
        sb.append("-");
        if(month > 9){
            sb.append(month + "-");
        }else{
            sb.append("0" + month + "-");
        }
        if(day > 9){
            sb.append(day);
        }else{
            sb.append("0" + day);
        }
        return sb.toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.submit:
//                if(mCommonChooseDialogClickListener != null){
//                    mCommonChooseDialogClickListener.onConfirm();
//                }
//                break;
            default:
                break;
        }
        getDialog().dismiss();
    }

    public interface DateChooseDialogClickListener {
        public void onDateChange(String date);
    }


    public static class DateChooseDialogBuilder {

        DateChooseDialogFragment mDateChooseDialogFragment;
        private String mTag;

        public DateChooseDialogBuilder(String date, int pageIdentify){
            mDateChooseDialogFragment = DateChooseDialogFragment.newInstance(date, pageIdentify);
            mTag = "date_choose";
        }

        public DateChooseDialogBuilder setChooseCallBack(DateChooseDialogClickListener commonChooseDialogClickListener){
            mDateChooseDialogFragment.setAlertDialogClickListener(commonChooseDialogClickListener);
            return this;
        }

        public DateChooseDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
//            mDateChooseDialogFragment.show(fragmentTransaction, mTag);
            fragmentTransaction.add(mDateChooseDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
